﻿using B.S.DataBase.MyProject.Domain;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using B.S.DataBase.MyRead.API.Appicant.Command;
using B.S.DataBase.MyRead.API.Appicant.Command.RoleCommand;
using MediatR;

namespace B.S.DataBase.MyRead.API.Appicant.CommandHandler.RoleCommandHandler
{
    public class GetRoleCommandHandler : IRequestHandler<GetRoleCommand, APIResult<List<Role>>>
    {
        private readonly IBaseRepository<Role> roleRepos;

        public GetRoleCommandHandler(IBaseRepository<Role> roleRepos)
        {
            this.roleRepos = roleRepos;
        }

        /// <summary>
        /// 角色显示
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public Task<APIResult<List<Role>>> Handle(GetRoleCommand request, CancellationToken cancellationToken)
        {
            APIResult<List<Role>> result = new APIResult<List<Role>>();
            var list = roleRepos.GetAll().Result.Where(x=>x.IsDeleted == false);
            if (list ==null)
            {
                result.Code = APIEnum.查询失败;
                result.Msg = APIEnum.查询失败.ToString();
            }else
            {
                result.Code = APIEnum.查询成功;
                result.Msg = APIEnum.查询成功.ToString();
                result.data = list.ToList();
            }
            return Task.FromResult(result);
        }
    }
}
